<!DOCTYPE HTML>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>

promise_test(function() {
  return navigator.mediaDevices.getUserMedia({audio: true})
      .then(function(s) {
    settings = s.getAudioTracks()[0].getSettings();
    assert_greater_than(Object.keys(settings).length, 0);
  });
}, 'An audio track returns settings');

promise_test(function() {
  return navigator.mediaDevices.getUserMedia({video: true})
      .then(function(s) {
    settings = s.getVideoTracks()[0].getSettings();
    assert_greater_than(Object.keys(settings).length, 0);
  });
}, 'A video track returns settings');

promise_test(function() {
  return navigator.mediaDevices.getUserMedia({audio: true})
      .then(function(s) {
    settings = s.getAudioTracks()[0].getSettings();
    assert_greater_than(Object.keys(settings).length, 0);
    assert_true('deviceId' in settings,
                'Device ID missing: ' + JSON.stringify(settings));
    assert_true('echoCancellation' in settings,
                'Echo cancellation missing: ' + JSON.stringify(settings));
    assert_true('autoGainControl' in settings,
                'Automatic gain control missing: ' + JSON.stringify(settings));
    assert_true('noiseSuppression' in settings,
                'Noise suppression missing: ' + JSON.stringify(settings));
  });
}, 'An audio track returns the expected variables');

promise_test(function() {
  return navigator.mediaDevices.getUserMedia({video: true})
      .then(function(s) {
    settings = s.getVideoTracks()[0].getSettings();
    assert_greater_than(Object.keys(settings).length, 0);
    assert_true('deviceId' in settings,
                'Device ID missing: ' + JSON.stringify(settings));
    assert_true('frameRate' in settings,
                'Frame rate missing: ' + JSON.stringify(settings));
    assert_true('width' in settings,
                'Width missing: ' + JSON.stringify(settings));
    assert_true('height' in settings,
                'Height missing: ' + JSON.stringify(settings));
    assert_true('aspectRatio' in settings,
                'Aspect ratio missing: ' + JSON.stringify(settings));
    assert_equals(settings.width / settings.height, settings.aspectRatio);
    assert_in_array(settings.resizeMode, [ "none", "crop-and-scale" ],
                'Invalid resizeMode: ' + JSON.stringify(settings));
  });
}, 'A video track returns the expected variables');

promise_test(function() {
  track1 = null;
  track2 = null;
  return navigator.mediaDevices.getUserMedia({video: true})
      .then(function(s1) {
    track1 = s1.getVideoTracks()[0];
    settings1 = track1.getSettings();
    // We ask for the second track to have half the width of the first one,
    // but the same source.
    // This should cause a scaling factor to be applied.
    constraints2 = {deviceId: settings1.deviceId,
                    width: { max: settings1.width / 2 }};
        console.log(JSON.stringify(constraints2));
    return navigator.mediaDevices.getUserMedia({video: constraints2});
  })
      .then(function(s) {
        track2 = s.getVideoTracks()[0];
        console.log(JSON.stringify(track2.getConstraints()));
    settings = track2.getSettings();
    settings1 = track1.getSettings();
    // This test does not work in blink_tests due to limitations in mocking.
    // The Web-Platform-Test that does the same thing passes when run
    // in a browser.
    // TODO(hta): Add constraints to the mock media stream registry.
    // crbug.com/617152
    // assert_equals(settings.deviceId, settings1.deviceId);
    // assert_equals(settings.width, settings1.width / 2,
    //              'widths are not 2x different: ' +
    //              JSON.stringify(settings) + ' ' + JSON.stringify(settings1));
  });
}, 'Two video tracks with the same source but different scaling are different');

promise_test(function() {
  return navigator.mediaDevices.getUserMedia({audio: true, video: true})
    .then(function(s) {
      videoSettings = s.getVideoTracks()[0].getSettings();
      audioSettings = s.getAudioTracks()[0].getSettings();
      assert_not_equals(videoSettings.deviceId, audioSettings.deviceId,
                        'video and audio device IDs should be different')
    });
}, 'Video and audio device IDs are different');

promise_test(function() {
  return navigator.mediaDevices.getUserMedia(
      {video: {facingMode: {exact: "user"}}})
      .then(function(s) {
    assert_unreached();
  })
      .catch(function(e) {
    assert_equals(e.name, 'OverconstrainedError');
  });
}, 'With no fake device, asking for user-facing device rejects');

promise_test(function() {
  return internals.addFakeDevice({kind: "video"},
                                 {facingMode: {exact: "user"}})
      .then(function() {
    navigator.mediaDevices.getUserMedia({video: {facingMode: {exact: "user"}}})
  })
      .then(function(s) {
    settings = s.getVideoTracks()[0].getSettings();
    assert_equals(settings.facingMode, "user");
  })
      .catch(function(e) {
    console.log('Fake devices are not functional yet.');
    // TODO(hta): Finish creation of fake devices. Until then, accept failure.
    // crbug.com/678561
    assert_equals(e.name, 'TypeError', "Check crbug.com/678561");
  });
}, 'With a fake user-facing device, facing mode is matched')

</script>
